﻿
Partial Class WebForm_ManageOrganization_AddOrganization
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        Me.fv.InsertItemTemplate = Me.fv.ItemTemplate
        Me.fv.EditItemTemplate = Me.fv.ItemTemplate
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.Visible = False

        If Not IsPostBack Then

        End If
    End Sub

    Protected Sub fv_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles fv.DataBound
        Me.BindDDLActive()

        If Me.fv.CurrentMode = FormViewMode.Insert Then
            'Me.BindDataForInsert()
        End If
    End Sub

    'Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click
    '    If Me.DataAccessMode = My.DataAccessMode.Insert Then
    '        Me.AddData()
    '        RaiseEvent OrganizationAdded(Me, New EventArgs)
    '    ElseIf Me.DataAccessMode = My.DataAccessMode.Update Then
    '        Me.UpdateData()
    '        RaiseEvent OrganizationUpdated(Me, New EventArgs)
    '    End If
    'End Sub

    Protected Sub fv_ItemCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewCommandEventArgs) Handles fv.ItemCommand
        If e.CommandName = "btnSave" Then
            If Me.DataAccessMode = My.DataAccessMode.Insert Then
                Me.AddData()
                RaiseEvent OrganizationAdded(Me, New EventArgs)
            ElseIf Me.DataAccessMode = My.DataAccessMode.Update Then
                Me.UpdateData()
                RaiseEvent OrganizationUpdated(Me, New EventArgs)
            End If

            RaiseEvent Saved(Me, New EventArgs)
        End If
    End Sub
End Class

Partial Class WebForm_ManageOrganization_AddOrganization
    Private Function AddData() As Boolean
        Dim dat As New clsData
        Dim sql As New StringBuilder

        Using scope As New Transactions.TransactionScope
            sql.Length = 0
            sql.AppendFormat("insert Organization (OrganizationName,Email,Parent,Active,Internal) select ")
            sql.AppendFormat(" '{0}' ", TryCast(Me.fv.FindControl("txtOrganizationName"), TextBox).Text)
            sql.AppendFormat(",'{0}' ", TryCast(Me.fv.FindControl("txtEmail"), TextBox).Text)
            sql.AppendFormat(",{0} ", Me.ParentID)
            sql.AppendFormat(",'{0}' ", "Y")
            sql.AppendFormat(",'{0}' ", TryCast(Me.fv.FindControl("ddlInternal"), DropDownList).SelectedValue)

            dat.AccData(sql.ToString(), Nothing, Data.CommandType.Text)

            Me.OrganizationID = MyObj.GetIdentity()
            Me.organizationNameField = TryCast(Me.fv.FindControl("txtOrganizationName"), TextBox).Text

            clsAuth.BuildOrganizationCode()
            clsAuth.BuildRoleOrganizationMappingData()

            scope.Complete()
        End Using

        Return True
    End Function

    Private Function UpdateData() As Boolean
        Dim dat As New clsData
        Dim sql As New StringBuilder

        Using scope As New Transactions.TransactionScope
            sql.Length = 0
            sql.AppendFormat("update Organization set ")
            sql.AppendFormat(" OrganizationName = '{0}' ", TryCast(Me.fv.FindControl("txtOrganizationName"), TextBox).Text)
            sql.AppendFormat(",Email = '{0}' ", TryCast(Me.fv.FindControl("txtEmail"), TextBox).Text)
            sql.AppendFormat(",Active = '{0}' ", TryCast(Me.fv.FindControl("ddlActive"), DropDownList).SelectedValue)
            sql.AppendFormat("where OrganizationID = {0} ", Me.OrganizationID)

            dat.AccData(sql.ToString(), Nothing, Data.CommandType.Text)

            Me.organizationNameField = TryCast(Me.fv.FindControl("txtOrganizationName"), TextBox).Text

            clsAuth.BuildOrganizationCode()
            clsAuth.BuildRoleOrganizationMappingData()

            scope.Complete()
        End Using

        Return True
    End Function

    Private Sub BindDDLActive()
        Dim ddlActive As DropDownList = TryCast(Me.fv.FindControl("ddlActive"), DropDownList)

        ddlActive.DataTextField = "Text"
        ddlActive.DataValueField = "Value"
        ddlActive.DataSource = MyObj.GetCodeMappingData("Organization", "Active")
        ddlActive.DataBind()

        'Me.ddlActive.SelectedIndex = Me.ddlActive.Items.IndexOf(Me.ddlActive.Items.FindByValue("Y"))
    End Sub

    ''' <summary>
    ''' 新增模式
    ''' </summary>
    ''' <param name="parentID"></param>
    ''' <remarks></remarks>
    Public Sub Show(ByVal parentID As String)
        Me.DataAccessMode = My.DataAccessMode.Insert
        Me.fv.ChangeMode(FormViewMode.Insert)
        Me.ParentID = parentID

        'Me.mpSearch.Show()
        Me.Visible = True
    End Sub

    ''' <summary>
    ''' 編輯模式
    ''' </summary>
    ''' <param name="organizationID"></param>
    ''' <remarks></remarks>
    Public Sub ShowEdit(ByVal organizationID As String)
        Me.DataAccessMode = My.DataAccessMode.Update
        Me.fv.ChangeMode(FormViewMode.Edit)
        Me.OrganizationID = organizationID

        Me.BindDataForEdit()

        'Me.mpSearch.Show()
        Me.Visible = True
    End Sub

    Private Sub BindDataForEdit()
        Dim dat As New clsData
        Dim sql As New StringBuilder

        sql.AppendFormat("select o.* from Organization o ")
        sql.AppendFormat("where o.OrganizationID = '{0}' ", Me.OrganizationID)

        Dim dt As Data.DataTable = dat.GetData(sql.ToString(), Nothing, Data.CommandType.Text, "dt").Tables(0)

        Me.fv.DataSource = dt
        Me.fv.DataBind()

        'TryCast(Me.fv.FindControl("txtOrganizationName"), TextBox).Enabled = False
        TryCast(Me.fv.FindControl("ddlActive"), DropDownList).Enabled = False
        TryCast(Me.fv.FindControl("ddlActive"), DropDownList).SelectedValue = dt.Rows(0)("Active")

        TryCast(Me.fv.FindControl("ddlInternal"), DropDownList).SelectedValue = dt.Rows(0)("Internal")
    End Sub

    Private organizationNameField As String

    Public ReadOnly Property OrganizationName() As String
        Get
            Return Me.organizationNameField
        End Get
    End Property

    Private Property ParentID() As String
        Get
            Return ViewState("ParentID")
        End Get
        Set(ByVal value As String)
            ViewState("ParentID") = value
        End Set
    End Property

    Public Property OrganizationID() As Integer
        Get
            Return ViewState("OrganizationID")
        End Get
        Set(ByVal value As Integer)
            ViewState("OrganizationID") = value
        End Set
    End Property

    Public Event OrganizationAdded(ByVal sender As Object, ByVal e As System.EventArgs)
    Public Event OrganizationUpdated(ByVal sender As Object, ByVal e As System.EventArgs)
    Public Event Saved(ByVal sender As Object, ByVal e As System.EventArgs)

    Private Property DataAccessMode() As My.DataAccessMode
        Get
            Return ViewState("DataAccessMode")
        End Get
        Set(ByVal value As My.DataAccessMode)
            ViewState("DataAccessMode") = value
        End Set
    End Property
End Class